/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.mime.Rfc822ValidationInputStream;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.FileSegmentDataSource;
/*     */ import com.zimbra.common.util.FileUtil;
/*     */ import com.zimbra.common.util.ListUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.Log.Level;
/*     */ import com.zimbra.common.util.LruMap;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.SystemUtil;
/*     */ import com.zimbra.common.util.TimeoutMap;
/*     */ import com.zimbra.common.util.TruncatingWriter;
/*     */ import com.zimbra.common.util.ValueCounter;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.File;
/*     */ import java.io.FileOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.Reader;
/*     */ import java.io.StringReader;
/*     */ import java.io.StringWriter;
/*     */ import java.io.Writer;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestUtilCode
/*     */   extends TestCase
/*     */ {
/*  55 */   private static final String NAME_PREFIX = TestUtilCode.class.getSimpleName();
/*     */   
/*     */   public void setUp() throws Exception
/*     */   {
/*  59 */     cleanUp();
/*     */   }
/*     */   
/*     */   public void testValueCounter() throws Exception
/*     */   {
/*  64 */     ValueCounter<String> vc = new ValueCounter();
/*  65 */     vc.increment("one");
/*  66 */     vc.increment("two");
/*  67 */     vc.increment("two");
/*  68 */     vc.increment("two");
/*  69 */     vc.decrement("two");
/*  70 */     vc.increment("three", 3);
/*     */     
/*  72 */     assertEquals("one", 1, vc.getCount("one"));
/*  73 */     assertEquals("two", 2, vc.getCount("two"));
/*  74 */     assertEquals("three", 3, vc.getCount("three"));
/*  75 */     assertEquals("total", 6, vc.getTotal());
/*  76 */     assertEquals("size", 3, vc.size());
/*     */     
/*  78 */     vc.clear();
/*     */     
/*  80 */     assertEquals("one", 0, vc.getCount("one"));
/*  81 */     assertEquals("two", 0, vc.getCount("two"));
/*  82 */     assertEquals("total", 0, vc.getTotal());
/*  83 */     assertEquals("size", 0, vc.size());
/*     */   }
/*     */   
/*     */   public void testTimeoutMap() throws Exception
/*     */   {
/*  88 */     ZimbraLog.test.debug("testTimeoutMap()");
/*  89 */     TimeoutMap<Integer, Integer> map = new TimeoutMap(500L);
/*     */     
/*     */ 
/*     */ 
/*  93 */     Map<Integer, Integer> timeouts = new HashMap();
/*  94 */     for (int i = 1; i <= 49; i++) {
/*  95 */       timeouts.put(Integer.valueOf(i), Integer.valueOf(i));
/*     */     }
/*  97 */     map.putAll(timeouts);
/*  98 */     for (int i = 50; i <= 99; i++) {
/*  99 */       map.put(Integer.valueOf(i), Integer.valueOf(i));
/*     */     }
/*     */     
/* 102 */     Integer oneHundred = Integer.valueOf(100);
/*     */     
/* 104 */     for (int i = 1; i <= 99; i++) {
/* 105 */       assertTrue("1: map does not contain key " + i, map.containsKey(Integer.valueOf(i)));
/* 106 */       assertTrue("1: map does not contain value " + i, map.containsValue(Integer.valueOf(i)));
/* 107 */       assertEquals("1: value for key " + i + " does not match", i, ((Integer)map.get(Integer.valueOf(i))).intValue());
/*     */     }
/*     */     
/* 110 */     assertEquals("1: Map size is incorrect", 99, map.size());
/* 111 */     assertFalse("1: map contains key 100", map.containsKey(oneHundred));
/* 112 */     assertFalse("1: map contains value 100", map.containsValue(oneHundred));
/* 113 */     assertNull("1: map value for key 100 is not null", map.get(oneHundred));
/*     */     
/* 115 */     Thread.sleep(700L);
/* 116 */     map.put(oneHundred, oneHundred);
/*     */     
/* 118 */     assertEquals("Map size is incorrect", 1, map.size());
/*     */     
/* 120 */     for (int i = 1; i <= 99; i++) {
/* 121 */       assertFalse("2: map contains key " + i, map.containsKey(Integer.valueOf(i)));
/* 122 */       assertFalse("2: map contains value " + i, map.containsValue(Integer.valueOf(i)));
/* 123 */       assertNull("2: value for key " + i + " is not null", map.get(Integer.valueOf(i)));
/*     */     }
/*     */     
/* 126 */     assertTrue("2: map does not contain key 100", map.containsKey(oneHundred));
/* 127 */     assertTrue("2: map does not contain value 100", map.containsValue(oneHundred));
/* 128 */     assertEquals("2: value for key 100 does not match", oneHundred, map.get(oneHundred));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testSplit()
/*     */     throws Exception
/*     */   {
/* 137 */     for (int i = 0; i < 50; i++) {
/* 138 */       List<Integer> list = new ArrayList();
/* 139 */       for (int j = 0; j < i; j++) {
/* 140 */         list.add(Integer.valueOf(j));
/*     */       }
/* 142 */       List<List<Integer>> listOfLists = ListUtil.split(list, 10);
/*     */       
/*     */ 
/* 145 */       int expectedSize = 0;
/* 146 */       if (list.size() > 0) {
/* 147 */         expectedSize = (list.size() - 1) / 10 + 1;
/*     */       }
/* 149 */       assertEquals("Unexpected number of splits for list of size " + list.size(), expectedSize, listOfLists.size());
/*     */       
/*     */ 
/*     */ 
/* 153 */       for (int j = 0; j < i; j++) {
/* 154 */         int listNum = j / 10;
/* 155 */         int index = j % 10;
/* 156 */         String context = String.format("j=%d, listNum=%d, index=%d", new Object[] { Integer.valueOf(j), Integer.valueOf(listNum), Integer.valueOf(index) });
/* 157 */         assertEquals(context, list.get(j), ((List)listOfLists.get(listNum)).get(index));
/*     */       }
/*     */       
/* 160 */       ZimbraLog.test.debug(String.format("Split a list of %d items into %d lists", new Object[] { Integer.valueOf(list.size()), Integer.valueOf(listOfLists.size()) }));
/* 161 */       assertTrue("Lists don't match: " + StringUtil.join(",", list), compareLists(list, listOfLists));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testInnermostException()
/*     */     throws Exception
/*     */   {
/* 170 */     assertNull(SystemUtil.getInnermostException(null));
/* 171 */     Exception inner = new Exception("inner");
/* 172 */     Exception middle = new Exception("middle", inner);
/* 173 */     Exception outer = new Exception("outer", middle);
/* 174 */     assertSame(inner, SystemUtil.getInnermostException(outer));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testSHA1Digest()
/*     */     throws Exception
/*     */   {
/* 182 */     byte[] data = "I am not a number.  I am a free man.".getBytes();
/* 183 */     String expected = "cc1ce56b9820cb5c4d6df9c9e39de0c7bf5b44a3";
/* 184 */     String expectedBase64 = "zBzla5ggy1xNbfnJ453gx79bRKM=";
/*     */     
/* 186 */     assertEquals(expected, ByteUtil.getSHA1Digest(data, false));
/* 187 */     assertEquals(expectedBase64, ByteUtil.getSHA1Digest(data, true));
/*     */     
/* 189 */     assertEquals(expected, ByteUtil.getSHA1Digest(new ByteArrayInputStream(data), false));
/* 190 */     assertEquals(expectedBase64, ByteUtil.getSHA1Digest(new ByteArrayInputStream(data), true));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testSHA256Digest()
/*     */     throws Exception
/*     */   {
/* 198 */     byte[] data = "I am not a number.  I am a free man.".getBytes();
/* 199 */     String expected = "a9be58e9f21ede0e9ddfac9fb3014e5d3552850cc1f0f2813ec7ccc232fde278";
/* 200 */     String expectedBase64 = "qb5Y6fIe3g6d36yfswFOXTVShQzB8PKBPsfMwjL94ng=";
/*     */     
/* 202 */     assertEquals(expected, ByteUtil.getSHA256Digest(data, false));
/* 203 */     assertEquals(expectedBase64, ByteUtil.getSHA256Digest(data, true));
/*     */     
/* 205 */     assertEquals(expected, ByteUtil.getSHA256Digest(new ByteArrayInputStream(data), false));
/* 206 */     assertEquals(expectedBase64, ByteUtil.getSHA256Digest(new ByteArrayInputStream(data), true));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testGetReaderContent()
/*     */     throws Exception
/*     */   {
/* 214 */     String s = "12345";
/* 215 */     assertEquals("", ByteUtil.getContent(new StringReader(s), 0, true));
/* 216 */     assertEquals("123", ByteUtil.getContent(new StringReader(s), 3, true));
/* 217 */     assertEquals("12345", ByteUtil.getContent(new StringReader(s), 5, true));
/* 218 */     assertEquals("12345", ByteUtil.getContent(new StringReader(s), 10, true));
/* 219 */     assertEquals("12345", ByteUtil.getContent(new StringReader(s), -1, true));
/*     */     
/* 221 */     Reader reader = new StringReader(s);
/* 222 */     ByteUtil.getContent(reader, 3, false);
/* 223 */     assertEquals("4", ByteUtil.getContent(reader, 1, true));
/*     */     try {
/* 225 */       ByteUtil.getContent(reader, 1, false);
/* 226 */       fail("IOException was not thrown");
/*     */     }
/*     */     catch (IOException e) {}
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testAccountLoggerNullAccountName()
/*     */     throws Exception
/*     */   {
/* 237 */     ZimbraLog.addAccountNameToContext(null);
/* 238 */     ZimbraLog.test.addAccountLogger(TestUtil.getAddress("user1"), Log.Level.info);
/* 239 */     ZimbraLog.test.debug("Testing addAccountNameToContext(null).");
/*     */   }
/*     */   
/*     */   public void testAccountLoggerMultipleAccountNames() throws Exception
/*     */   {
/* 244 */     String address1 = TestUtil.getAddress("user1");
/* 245 */     String address2 = TestUtil.getAddress("user2");
/*     */     
/* 247 */     ZimbraLog.addAccountNameToContext(address1);
/* 248 */     ZimbraLog.addAccountNameToContext(address2);
/* 249 */     Set<String> names = ZimbraLog.getAccountNamesFromContext();
/* 250 */     assertEquals(1, names.size());
/* 251 */     assertTrue(names.contains(address2));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testGzip()
/*     */     throws Exception
/*     */   {
/* 259 */     String s = "Put the message in a box";
/* 260 */     byte[] original = s.getBytes();
/* 261 */     byte[] compressed = ByteUtil.compress(original);
/* 262 */     byte[] uncompressed = ByteUtil.uncompress(compressed);
/*     */     
/* 264 */     assertFalse(ByteUtil.isGzipped(original));
/* 265 */     assertTrue(ByteUtil.isGzipped(compressed));
/* 266 */     assertFalse(ByteUtil.isGzipped(uncompressed));
/* 267 */     assertEquals(s, new String(uncompressed));
/*     */     
/*     */ 
/* 270 */     File file = File.createTempFile("TestUtilCode.testGzip-uncompressed", null);
/* 271 */     FileOutputStream out = new FileOutputStream(file);
/* 272 */     out.write(original);
/* 273 */     out.close();
/* 274 */     assertFalse(FileUtil.isGzipped(file));
/* 275 */     file.delete();
/*     */     
/*     */ 
/* 278 */     file = File.createTempFile("TestUtilCode.testGzip-compressed", null);
/* 279 */     out = new FileOutputStream(file);
/* 280 */     out.write(compressed);
/* 281 */     out.close();
/* 282 */     assertTrue(FileUtil.isGzipped(file));
/* 283 */     file.delete();
/*     */   }
/*     */   
/*     */   public void testRfc822Validation() throws Exception
/*     */   {
/* 288 */     validateRfc822("A", 0, null, true);
/* 289 */     validateRfc822("A", 1, null, true);
/* 290 */     validateRfc822(getLongString(10240), 0, null, true);
/* 291 */     validateRfc822(getLongString(10241), 0, null, false);
/* 292 */     validateRfc822("A: B\r\n" + getLongString(10240), 0, null, true);
/* 293 */     validateRfc822("A: B\r\n", 10, getLongString(10230), true);
/* 294 */     validateRfc822("A: B\r\n", 10240, "\r\nxyz", true);
/* 295 */     validateRfc822("A: B\r\n", 10241, "\r\nxyz", false);
/* 296 */     validateRfc822("A: B\r\n", 10240, "xyz", false);
/*     */   }
/*     */   
/*     */   private String getLongString(int length) {
/* 300 */     byte[] content = new byte[length];
/* 301 */     for (int i = 0; i < length; i++) {
/* 302 */       content[i] = ((byte)(97 + i % 26));
/*     */     }
/* 304 */     return new String(content);
/*     */   }
/*     */   
/*     */   private void validateRfc822(String before, int numNullBytes, String after, boolean isValid) throws IOException
/*     */   {
/* 309 */     byte[] content = getBytes(before, numNullBytes, after);
/*     */     
/*     */ 
/* 312 */     byte[] buf = new byte[100];
/* 313 */     Rfc822ValidationInputStream in = new Rfc822ValidationInputStream(new ByteArrayInputStream(content), 10240L);
/* 314 */     while (in.read(buf) >= 0) {}
/*     */     
/* 316 */     in.close();
/* 317 */     assertEquals(isValid, in.isValid());
/*     */     
/*     */ 
/* 320 */     in = new Rfc822ValidationInputStream(new ByteArrayInputStream(content), 10240L);
/* 321 */     while (in.read() >= 0) {}
/*     */     
/* 323 */     in.close();
/* 324 */     assertEquals(isValid, in.isValid());
/*     */     
/*     */ 
/* 327 */     byte[] copy = ByteUtil.getContent(new Rfc822ValidationInputStream(new ByteArrayInputStream(content), 10240L), content.length);
/* 328 */     for (int i = 0; i < content.length; i++) {
/* 329 */       assertEquals("Mismatch at byte " + i, content[i], copy[i]);
/*     */     }
/*     */   }
/*     */   
/*     */   private byte[] getBytes(String before, int numNullBytes, String after) {
/* 334 */     if (before == null) {
/* 335 */       before = "";
/*     */     }
/* 337 */     if (after == null) {
/* 338 */       after = "";
/*     */     }
/* 340 */     byte[] beforeBytes = before.getBytes();
/* 341 */     byte[] afterBytes = after.getBytes();
/* 342 */     byte[] content = new byte[beforeBytes.length + numNullBytes + afterBytes.length];
/* 343 */     System.arraycopy(beforeBytes, 0, content, 0, beforeBytes.length);
/* 344 */     System.arraycopy(afterBytes, 0, content, beforeBytes.length + numNullBytes, afterBytes.length);
/* 345 */     return content;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testTruncatingWriter()
/*     */     throws Exception
/*     */   {
/* 353 */     doTruncatingWriterTest(0);
/* 354 */     doTruncatingWriterTest(5);
/* 355 */     doTruncatingWriterTest(100);
/*     */   }
/*     */   
/*     */   private void doTruncatingWriterTest(int maxChars) throws Exception
/*     */   {
/* 360 */     String original = "Come talk to me";
/* 361 */     StringWriter sw = new StringWriter();
/* 362 */     Writer w = new TruncatingWriter(sw, maxChars);
/* 363 */     w.append(original);
/* 364 */     String s = sw.toString();
/* 365 */     int actualChars = Math.min(maxChars, original.length());
/* 366 */     assertEquals(actualChars, s.length());
/* 367 */     assertEquals(original.substring(0, actualChars), s);
/*     */   }
/*     */   
/*     */   private static <E> boolean compareLists(List<E> list, List<List<E>> listOfLists) {
/* 371 */     int i = 0;
/* 372 */     for (List<E> curList : listOfLists) {
/* 373 */       for (E item : curList) {
/* 374 */         if (!item.equals(list.get(i))) {
/* 375 */           return false;
/*     */         }
/* 377 */         i++;
/*     */       }
/*     */     }
/* 380 */     return true;
/*     */   }
/*     */   
/*     */   public void testFileSegmentDataSource()
/*     */     throws Exception
/*     */   {
/* 386 */     File file = File.createTempFile(NAME_PREFIX, null);
/* 387 */     String content = "12345";
/* 388 */     FileOutputStream out = new FileOutputStream(file);
/* 389 */     out.write(content.getBytes());
/* 390 */     out.close();
/*     */     
/* 392 */     FileSegmentDataSource ds = new FileSegmentDataSource(file, 0L, 5L);
/* 393 */     assertEquals("12345", new String(ByteUtil.getContent(ds.getInputStream(), 5)));
/* 394 */     assertEquals("12345", new String(ByteUtil.getContent(ds.getInputStream(), 5)));
/*     */     
/* 396 */     ds = new FileSegmentDataSource(file, 1L, 3L);
/* 397 */     assertEquals("234", new String(ByteUtil.getContent(ds.getInputStream(), 5)));
/*     */     
/* 399 */     file.delete();
/*     */   }
/*     */   
/*     */   private class LruTest<K, V>
/*     */     extends LruMap<K, V>
/*     */   {
/*     */     K lastRemovedKey;
/*     */     V lastRemovedValue;
/*     */     
/*     */     LruTest(int maxSize)
/*     */     {
/* 410 */       super();
/*     */     }
/*     */     
/* 413 */     K getLastRemovedKey() { return (K)this.lastRemovedKey; }
/* 414 */     V getLastRemovedValue() { return (V)this.lastRemovedValue; }
/*     */     
/*     */     protected void willRemove(K key, V value)
/*     */     {
/* 418 */       this.lastRemovedKey = key;
/* 419 */       this.lastRemovedValue = value;
/*     */     }
/*     */   }
/*     */   
/*     */   public void testLruMap() throws Exception
/*     */   {
/* 425 */     LruTest<Integer, String> map = new LruTest(3);
/* 426 */     map.put(Integer.valueOf(1), "one");
/* 427 */     map.put(Integer.valueOf(2), "two");
/* 428 */     map.put(Integer.valueOf(3), "three");
/* 429 */     map.put(Integer.valueOf(4), "four");
/* 430 */     assertEquals(1, ((Integer)map.getLastRemovedKey()).intValue());
/* 431 */     assertEquals("one", (String)map.getLastRemovedValue());
/* 432 */     assertFalse(map.containsKey(Integer.valueOf(1)));
/*     */     
/* 434 */     map.get(Integer.valueOf(2));
/* 435 */     map.put(Integer.valueOf(5), "five");
/* 436 */     assertEquals(3, ((Integer)map.getLastRemovedKey()).intValue());
/* 437 */     assertEquals("three", (String)map.getLastRemovedValue());
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/* 442 */     cleanUp();
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception
/*     */   {
/* 447 */     ZimbraLog.test.removeAccountLogger(TestUtil.getAddress("user1"));
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 452 */     TestUtil.cliSetup();
/* 453 */     TestUtil.runTest(TestUtilCode.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestUtilCode.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */